package com.cw.jeeyt.service.impl.process;

import java.util.ArrayList;
import java.util.List;

import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.idm.api.Group;
import org.flowable.idm.engine.impl.GroupQueryImpl;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntity;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;
import org.flowable.idm.engine.impl.persistence.entity.UserEntity;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;

import com.google.common.collect.Lists;
import com.cw.jeeyt.common.StringUtils;
import com.cw.jeeyt.common.utils.SpringContextHolder;
import com.cw.jeeyt.dao.domain.security.SecurityRoleModel;
import com.cw.jeeyt.dao.intf.security.SecurityRoleDao;
import com.cw.jeeyt.dao.intf.security.SecurityUserDao;
import com.cw.jeeyt.dao.model.security.SecurityUserDo;
import com.cw.lang.common.utils.CollectionUtil;

/**
 * 扩展Flowable组Service
 *
 * @author liuruijun
 * @version 2018-08-02
 */
public class FlowGroupQueryImpl extends GroupQueryImpl {

    private static final long serialVersionUID = 1L;

    private SecurityUserDao userDao;
    private SecurityRoleDao roleDao;

    public static UserEntity toFlowableUser(SecurityUserDo user) {
        if (user == null) {
            return null;
        }
        UserEntity userEntity = new UserEntityImpl();
        userEntity.setId(user.getUsername());
        userEntity.setFirstName(user.getUsername());
        userEntity.setLastName(StringUtils.EMPTY);
        userEntity.setPassword(user.getPassword());
        userEntity.setEmail(user.getEmail());
        userEntity.setRevision(1);
        return userEntity;
    }

    public static GroupEntity toFlowableGroup(SecurityRoleModel role) {
        if (role == null) {
            return null;
        }
        GroupEntity groupEntity = new GroupEntityImpl();
        groupEntity.setId(role.getName());
        groupEntity.setName(role.getName());
        groupEntity.setType(role.getRoleType());
        groupEntity.setRevision(1);
        return groupEntity;
    }

    public SecurityUserDao getUserDao() {
        if (userDao == null) {
            userDao = SpringContextHolder.getBean(SecurityUserDao.class);
        }
        return userDao;
    }

    public SecurityRoleDao getRoleDao() {
        if (roleDao == null) {
            roleDao = SpringContextHolder.getBean(SecurityRoleDao.class);
        }
        return roleDao;
    }

    @Override
    public long executeCount(CommandContext commandContext) {
        return executeQuery().size();
    }

    @Override
    public List<Group> executeList(CommandContext commandContext) {
        return executeQuery();
    }

    protected List<Group> executeQuery() {
        if (getUserId() != null) {
            return findGroupsByUser(getUserId());
        } else {
            return findAllGroups();
        }
    }

    protected List<Group> findGroupsByUser(String userId) {
        List<Group> list = Lists.newArrayList();
        SecurityUserDo user = getUserDao().getUserByUserId(userId);
        List<SecurityRoleModel> roleModels = getRoleDao().getRolesByUserId(user.getId());
        if (user != null && CollectionUtil.isNotEmpty(roleModels)) {
            for (SecurityRoleModel role : roleModels) {
                list.add(toFlowableGroup(role));
            }
        }

        return list;
    }

    protected List<Group> findAllGroups() {
        return new ArrayList<>();
    }
}
